package com.sevntu.apvs.duelement;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;


public class FindCreateElement {

	String type;
	String comp;
	String nIn;
	String nOut;
	String time;
	String Ncont;
	String tabI;
	List<String> list;
	public Element FindElement(String name)
    {//filling the fields of the object
        Element ob= null;
        int o=-1;
        o=readFile(name);
        if(o==0){ ob=new Element(comp,type,0,Integer.parseInt(nIn),Integer.parseInt(nOut),time,Ncont,tabI);}
        return ob;
     }

	 public int readFile(String name)
	    {// searching element by name from the library
		 String line;
	    int o=-1;
	        try{
	        	BufferedReader in = new BufferedReader(new FileReader("src/com/sevntu/apvs/duelement/du_lib/lib.txt"));
	        	line= in.readLine();
	        	while(line!=null){
	               
	                int nB =line.indexOf(">"+name+"<");
	                if(nB==-1){line= in.readLine();}
	                if(nB!=-1)
	                {// filling the fields of the object
	                	o=0;
	                	type = line.substring(nB+1, (nB+1+name.length()));
	                	line= in.readLine();
	                	int n1=line.indexOf(">");
	                	int n2=line.indexOf("</");
	                	comp =line.substring(n1+1, (n2));
	                	line= in.readLine();
	                	n1=line.indexOf(">");
	                	n2=line.indexOf("</");
	                	nIn =line.substring(n1+1, (n2));
	                	line= in.readLine();
	                	n1=line.indexOf(">");
	                	n2=line.indexOf("</");
	                	nOut =line.substring(n1+1, (n2));
	                	line= in.readLine();
	                	n1=line.indexOf(">");
	                	n2=line.indexOf("</");
	                	time =line.substring(n1+1, (n2));
	                	line= in.readLine();
	                	n1=line.indexOf(">");
	                	n2=line.indexOf("</");
	                	Ncont =line.substring(n1+1, (n2));
	                	line= in.readLine();
	                	n1=line.indexOf(">");
	                	n2=line.indexOf("</");
	                	tabI =line.substring(n1+1, (n2));
	                	line = null;
	                }
	            }

	        }
	       catch(Exception e){}
	       return o;
	}
	 
	 public List<String> CreateListElement()
	    {// list of elements present in the library
		 String line; int o=-1;
	    list= new ArrayList <String>();
	    try{
	        	BufferedReader in = new BufferedReader(new FileReader("src/com/sevntu/apvs/duelement/du_lib/lib.txt"));
	        	line= in.readLine();
	        	while(line!=null){
	               
	                int nB =line.indexOf("<TYPE>");
	                if(nB==-1){line= in.readLine();}
	                if(nB!=-1)
	                {
	                	o=0;
	                	int n=line.indexOf("</");
	                	list.add(line.substring(7,n));
	                	line= in.readLine();
	                	
	                }
	            }

	        }
	       catch(Exception e){}
	       return list;
	}


}
